package com.moshang.mall.member.controller;

import com.moshang.mall.exception.BizCodeEnume;
import com.moshang.mall.member.entity.MemberEntity;
import com.moshang.mall.member.feign.CouponFeignService;
import com.moshang.mall.member.service.MemberService;
import com.moshang.mall.member.vo.MemberLoginVo;
import com.moshang.mall.utils.PageUtils;
import com.moshang.mall.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author moshang
 * @email 597575122@qq.com
 * @date 2020-09-01 20:16:24
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Resource
    private MemberService memberService;


    @Resource
    private CouponFeignService couponFeignService;
    @RequestMapping("coupons")
    public R getCoupons(){
        MemberEntity memberEntity=new MemberEntity();
        memberEntity.setNickname("墨殇");
        R r = couponFeignService.membercoupons();
        return R.ok().put("member",memberEntity).put("coupon",r.get("coupons"));
    }

    @PostMapping("login")
    public R login(@RequestBody MemberLoginVo vo){
       MemberEntity member=memberService.login(vo);
       if (member!=null){
           return R.ok().put("data",member);
       }
       return R.error(BizCodeEnume.LOGINACCOUNT_PASSWORD_INVATILD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCOUNT_PASSWORD_INVATILD_EXCEPTION.getMsg());

    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
